﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Inventory;
using NotebookS2.Sales.Dialogs; 

namespace NotebookS2
{
    public partial class FND_ITM_WH : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public FND_ITM_WH()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string SourceWarehouse { get; set; }
        
        public string Code { get; set; }
        public string BarCode { get; set; }
        public string Description { get; set; }
        public string UOM { get; set; }
        public string UnitCost { get; set; }
        public string LotNumber { get; set; }
        public string ExpiryDate { get; set; }
        public string ManufacturingDate { get; set; }


        List<string> _ItemCodes = new List<string>();

        


        private void FND_ITM_WH_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {

            int iFocusedRowHandle = gridView1.FocusedRowHandle;

            this.Code = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["ItemCode"]).ToString();
            this.BarCode = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["BarCode"]).ToString();
            this.Description = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["ItemName"]).ToString();
            this.UOM = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["UoM"]).ToString();
            this.UnitCost = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["UnitCost"]).ToString();
            this.LotNumber = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["LotNumber"]).ToString();
            this.ExpiryDate = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["ExpiryDate"]).ToString();
            this.ManufacturingDate = gridView1.GetRowCellValue(iFocusedRowHandle, gridView1.Columns["MfrDate"]).ToString();
            
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvwSearched_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void lvwSearched_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void txtSearch_DoubleClick(object sender, EventArgs e)
        {
           
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void txtSearch_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSearch_ButtonClick(sender, null);

                if (gridView1.RowCount > 0)
                {
                    gridView1.Focus();
                }
            }
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm5));

            StockManager oManager = new StockManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable dt = new DataTable();
            if (cboSearch.SelectedIndex == 0)
            {
                dt = oManager.BrowseItemCodeAvailableStock(this.SourceWarehouse, txtSearch.Text);
            }
            else
            {
                dt = oManager.BrowseItemNameAvailableStock(this.SourceWarehouse, txtSearch.Text);
            }

            oManager.Close();

            var items = from row in dt.AsEnumerable()
                        select new
                        {
                            ItemCode = row["ItemCode"].ToString(),
                            BarCode = row["BarCode"].ToString(),
                            ItemName = row["ItemName"].ToString(),
                            Qty = Convert.ToDouble(row["Qty"]),
                            UoM = row["UoM"].ToString(),
                            UnitCost = row["UnitCost"].ToString(),
                            LotNumber = row["LotNumber"].ToString(),
                            ExpiryDate = row["ExpiryDate"] != DBNull.Value ? Convert.ToDateTime(row["ExpiryDate"]).ToShortDateString() : null,
                            MfrDate = row["MfrDate"] != DBNull.Value ? Convert.ToDateTime(row["MfrDate"]).ToShortDateString() : null
                        };

            gridControl1.DataSource = items;
            gridControl1.Refresh();

            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }
    }
}
